package net.onceface.application.assembler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.onceface.application.dto.PersonDTO;
import net.onceface.core.domain.Person;
import net.onceface.core.domain.enums.Gender;

public class PersonAssembler {
	
	/**
	 * 传入数据库对象和前端传入对象，返回最终修改对象
	 * @param PersonDTO person 数据库对象
	 * @param PersonDTO personDTO 前端传入对象
	 * **/
	public static PersonDTO toUpdataDTO(PersonDTO person, PersonDTO personDTO){
		if(personDTO.getName() != null){
			person.setName(personDTO.getName());
		}
		if(personDTO.getGender() != null){
			person.setGender(personDTO.getGender());
		}
		if(personDTO.getIdNumber() != null){
			person.setIdNumber(personDTO.getIdNumber());
		}
		if(personDTO.getMobilePhone() != null){
			person.setMobilePhone(personDTO.getMobilePhone());
		}
		if(personDTO.getFamilyPhone() != null){
			person.setFamilyPhone(personDTO.getFamilyPhone());
		}
		if(personDTO.getEmail() != null){
			person.setEmail(personDTO.getEmail());
		}
		if(personDTO.getQq() != null){
			person.setQq(personDTO.getQq());
		}
		if(personDTO.getAddress() != null){
			person.setAddress(personDTO.getAddress());
		}
		if(personDTO.getBlog() != null){
			person.setBlog(personDTO.getBlog());
		}
		if(personDTO.getBirthday() != null){
			person.setBirthday(personDTO.getBirthday());
		}
		return person;
	}

	public static PersonDTO toDTO(Person person){
		PersonDTO result = new PersonDTO();
		if(person == null){
			return null;
		}
		result.setId(person.getId());
		result.setVersion(person.getVersion());
		result.setEmail(person.getEmail());
		result.setBirthday(person.getBirthday());
		result.setBlog(person.getBlog());
		result.setAddress(person.getAddress());
		result.setIdNumber(person.getIdNumber());
		result.setMobilePhone(person.getMobilePhone());
		result.setFamilyPhone(person.getMobilePhone());
		result.setGender(person.getGender().getLabel());
		result.setQq(person.getQq());
		result.setName(person.getName());
		return result;
	}
	
	public static Person toEntity(PersonDTO person){
		Person result = new Person();
		if(person == null){
			return null;
		}
		result.setId(person.getId());
		result.setVersion(person.getVersion());
		result.setEmail(person.getEmail());
		result.setBirthday(person.getBirthday());
		result.setBlog(person.getBlog());
		result.setAddress(person.getAddress());
		result.setIdNumber(person.getIdNumber());
		result.setMobilePhone(person.getMobilePhone());
		result.setFamilyPhone(person.getMobilePhone());
		if(person.getGender().equals("男")){
			result.setGender(Gender.MALE);
		}else{
			result.setGender(Gender.FEMALE);
		}
		result.setQq(person.getQq());
		result.setName(person.getName());
		return result;
	}
	
	public static List<PersonDTO>  toDTOs(Collection<Person>  persons){
		if (persons == null) {
			return null;
		}
		List<PersonDTO> results = new ArrayList<PersonDTO>();
		for (Person each : persons) {
			results.add(toDTO(each));
		}
		return results;
	}
	
	public static List<Person>  toEntities(Collection<PersonDTO>  persons){
		if (persons == null) {
			return null;
		}
		List<Person> results = new ArrayList<Person>();
		for (PersonDTO each : persons) {
			results.add(toEntity(each));
		}
		return results;
	}
}
